package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.mapper.*;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IProjectLogDetailService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 竞标记录详细Service业务层处理
 *
 * @author ruoyi
 * @date 2024-09-04
 */
@Service
public class ProjectLogDetailServiceImpl implements IProjectLogDetailService {
    @Resource
    private ProjectLogDetailMapper projectLogDetailMapper;
    @Resource
    private TypeMapper typeMapper;
    @Resource
    private SpecMapper specMapper;
    @Resource
    private BrandMapper brandMapper;
    @Resource
    private ProjectDetailMapper projectDetailMapper;
    @Resource
    private ProjectLogMapper projectLogMapper;

    /**
     * 查询竞标记录详细
     *
     * @param id 竞标记录详细主键
     * @return 竞标记录详细
     */
    @Override
    public ProjectLogDetail selectProjectLogDetailById(Long id) {
        return projectLogDetailMapper.selectProjectLogDetailById(id);
    }

    /**
     * 查询竞标记录详细列表
     *
     * @param projectLogDetail 竞标记录详细
     * @return 竞标记录详细
     */
    @Override
    public List<ProjectLogDetail> selectProjectLogDetailList(ProjectLogDetail projectLogDetail) {
        ProjectLog projectLog;
        if (projectLogDetail.getLogId() != null) {
            projectLog = projectLogMapper.selectProjectLogById(projectLogDetail.getLogId());
        } else {
            projectLog = null;
        }
        List<ProjectLogDetail> details = projectLogDetailMapper.selectProjectLogDetailList(projectLogDetail);
        details.forEach(item -> {
            if (projectLog != null) {
                item.setXgcs(projectLog.getXgcs());
            }
            if (item.getTypeId() != null) {
                Type type = typeMapper.selectTypeById(item.getTypeId());
                if (type != null) {
                    item.setTypeName(type.getName());
                }
            }
            if (item.getSpecId() != null) {
                Spec spec = specMapper.selectSpecById(item.getSpecId());
                if (spec != null) {
                    item.setSpecName(spec.getName());
                }
            }
            if (item.getBrandId() != null) {
                Brand brand = brandMapper.selectBrandById(item.getBrandId());
                if (brand != null) {
                    item.setBrandName(brand.getName());
                }
            }
            List<ProjectDetail> detailList = projectDetailMapper.selectProjectDetailList(new ProjectDetail() {{
                setProjectId(item.getProjectId());
                setTypeId(item.getTypeId());
                setSpecId(item.getSpecId());
                setBrandId(item.getBrandId());
            }});
            if (detailList != null && !detailList.isEmpty()) {
                item.setPriceLimit(detailList.get(0).getPrice());
            }


        });

        return details;
    }

    /**
     * 新增竞标记录详细
     *
     * @param projectLogDetail 竞标记录详细
     * @return 结果
     */
    @Override
    public int insertProjectLogDetail(ProjectLogDetail projectLogDetail) {
        projectLogDetail.setCreateTime(DateUtils.getNowDate());
        return projectLogDetailMapper.insertProjectLogDetail(projectLogDetail);
    }

    /**
     * 修改竞标记录详细
     *
     * @param projectLogDetail 竞标记录详细
     * @return 结果
     */
    @Override
    @Transactional
    public int updateProjectLogDetail(List<ProjectLogDetail> details) {

        if (details == null || details.isEmpty()) {
            throw new ServiceException("未选择详细");
        }
        AtomicInteger i = new AtomicInteger();
        details.forEach(item -> {
            item.setUpdateTime(DateUtils.getNowDate());
            i.set(projectLogDetailMapper.updateProjectLogDetail(item));
        });


        ProjectLog projectLog = projectLogMapper.selectProjectLogById(details.get(0).getLogId());
        if (projectLog != null) {
            if (projectLog.getXgcs() > 0) {
                throw new ServiceException("修改次数受限");
            } else {
                projectLog.setXgcs(projectLog.getXgcs() + 1);
                projectLogMapper.updateProjectLog(projectLog);
            }
        }

        return i.get();
    }

    /**
     * 批量删除竞标记录详细
     *
     * @param ids 需要删除的竞标记录详细主键
     * @return 结果
     */
    @Override
    public int deleteProjectLogDetailByIds(Long[] ids) {
        return projectLogDetailMapper.deleteProjectLogDetailByIds(ids);
    }

    /**
     * 删除竞标记录详细信息
     *
     * @param id 竞标记录详细主键
     * @return 结果
     */
    @Override
    public int deleteProjectLogDetailById(Long id) {
        return projectLogDetailMapper.deleteProjectLogDetailById(id);
    }
}
